<?
	$usr = $this->usr;
	global $usr_groups, $usr_data;
?>
	function flagChange(){
		$('changesbutton').disabled = false;
		window.onbeforeunload = function(e){
		  var s = 'You have unsaved changes.';
		  if (e) e.returnValue = s;
		  return s;
		};
	}
	
	function unflagChange(){
		window.onbeforeunload = function(){};
	}
	
	var User = {
		load:function(o){
			for(var k in o) this[k] = o[k];
			if(this['class'] != 'Root'){
				$('usrname').value = $('gpname').innerHTML = this.name;
				$('usrclass').value = this['class'];
			}
			$('usremail').value = this.email
			$('usrdom').innerHTML = this.home;
			['script','apache','symlink'].each(function(p){ $('usr_can_'+p).checked = User['can_'+p]; });
		},
		save:function(){
			var email = $('usremail').value.trim();
			if(!email) return TUXalert("The email is blank");
			var pass = $('usrpass').value.trim();
			if(pass){
				var pass2 = $('usrpass2').value.trim();
				if(pass !== pass2) return TUXalert("The passwords do not match");
				var chk = {
					cap:pass.match(/[A-Z]/),
					low:pass.match(/[a-z]/),
					num:pass.match(/[0-9]/),
					oth:pass.match(/[^A-Za-z0-9]/)
				};
				var score = 0;
				for(var k in chk) if(chk[k]) score++;
				if(score < 2) return TUXalert("The password needs 2 of the following qualities:<br /><ul><li>1 lowercase letter</li><li>1 uppercase letter</li><li>1 number</li><li>or 1 special character</li></ul>");
			}
			var args = {id:User.id};
			<? if($usr->class !== 'Root') { ?> 
			var name = $('usrname').value.trim();
			if(!name || (name.length < 4)) return TUXalert("The name cannot be less than 4 characters");
			if(name != User.name) args.name = name;
			var uclass = $('usrclass').value;
			if(uclass != User['class']){
				if(!confirm('Are you sure you want to alter the class of this user?')) return;
				args['class'] = uclass;
			}
			args.groups = Groups.get();
			<? } ?>
			if(this['class'] == 'Sudoer') ['script','apache','symlink'].each(function(p){ args['can_'+p] = $('usr_can_'+p).checked; });
			if(email != User.email) args.email = email;
			if(pass) args.pass = hex_sha1(pass);
			args.home = User.home;
			ajaxExec('saveUser', args);
			unflagChange();
		}
	};
	
	<? if($usr->class !== 'Root') { ?> 
	
	var Groups = {
		ugs:{},
		load:function(o){
			for(var k in o) Groups.ugs[k] = o[k];
			Groups.draw()
		},
		add:function(id, app){
			Groups.ugs[id] = {'id':id, 'name':app.name};
			Groups.draw();
			flagChange();
		},
		remove:function(id){
			var gs = Groups.ugs;
			Groups.ugs = {};
			for(var k in gs) if(k != id) Groups.ugs[k] = gs[k];
			Groups.draw();
			flagChange();
		},
		draw:function(){
			$('usrgroups').innerHTML = '';
			for(var k in this.ugs){
				$('usrgroups').innerHTML += '<br />' + this.ugs[k].name
				+ ' <a style="font-size:0.8em" href="javascript:Groups.remove(\'' + k + '\')">remove</a><br />';
			}
		},
		get:function(){
			var u = [];
			for(var k in Groups.ugs) u.push(k);
			return u.join(',');
		},
		browse:function(){
			Browser.goto('../groups', {title:'Add user to group:', xargs:{filter:'g500,<?=$usr->grp?>'}});
			AppsB.clickHandler = function(){
				Groups.add(this.id, this);
				Browser.hide();
			}	
		}
	}
	<? } ?>
	
	var Domains = {
		change:function(id, dom){
			User.home = id;
			$('usrdom').innerHTML = id;
			flagChange()
		},
		browse:function(){
			Browser.goto('../domains', {title:'Change home domain of of ' + User.name + ' to:'});
			AppsB.clickHandler = function(){
				Domains.change(this.id);
				Browser.hide();
			}
		}
	};

	window.queue(function(){
		Chrome.load({
			Selection:0
		});
		User.load(<?=json_encode($usr_data)?>);
		<? if($usr->class !== 'Root') { ?> 
		Groups.load(<?=serializeGroups($usr_groups)?>);
		<? } ?> 
	});
